<template>
  <div class="app-container">
    <!--表单组件-->
    <el-dialog
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="760px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="标题" prop="name">
          <el-input v-model.trim="form.name" style="width: 605px;" />
        </el-form-item>
        <!-- <el-form-item label="发布者">
             <el-input v-if="false" v-show="false" v-model="form.userId" />
             <el-input v-model="form.publishUserName" style="width: 135px;padding-left:-5px"/>
           </el-form-item>-->
        <el-form-item label="内容">
          <el-input v-model="form.content" :rows="3" type="textarea" style="width: 605px;" />
        </el-form-item>
        <el-form-item label="活动开始日期">
          <el-date-picker v-model="form.startTime" type="datetime" format="yyyy-MM-dd HH:mm" style="width: 218px;" />
        </el-form-item>
        <el-form-item label="活动结束日期" style="padding-left: 55px;">
          <el-date-picker v-model="form.endTime" type="datetime" format="yyyy-MM-dd HH:mm" style="width: 218px;" />
        </el-form-item>
        <el-form-item label="预约开始时间">
          <el-date-picker v-model="form.appStartTime" type="datetime" format="yyyy-MM-dd HH:mm" style="width: 218px;" />
        </el-form-item>
        <el-form-item label="预约结束时间" style="padding-left: 55px;">
          <el-date-picker v-model="form.appEndTime" type="datetime" format="yyyy-MM-dd HH:mm" style="width: 218px;" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="form.sort" style="width: 218px;" />
        </el-form-item>
        <el-form-item label="备注" style="padding-left: 55px;">
          <el-input v-model="form.remark" style="width: 217px;" />
        </el-form-item>
        <el-form-item v-permission="permission.verify" label="审核状态">
          <el-select v-model="form.verifyStatus" filterable placeholder="请选择" style="width: 218px;">
            <el-option v-for="item in dict.info_verify_status" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" style="padding-left: 70px">
          <el-radio v-for="item in dict.data_status" :key="item.id" v-model="form.isEnable" :label="item.value">
            {{ item.label }}
          </el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <el-row style="margin-bottom: 10px;">
      <el-col :span="24">
        <el-card class="box-card">
          <div v-if="crud.props.searchToggle">
            <!-- 搜索 -->
            <label class="el-form-item-label">标题</label>
            <el-input
              v-model="query.name"
              clearable
              placeholder="标题"
              style="width: 160px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
            <label class="el-form-item-label">审核状态</label>
            <el-select
              v-model="query.verifyStatus"
              clearable
              size="small"
              placeholder="审核状态"
              class="filter-item"
              style="width: 120px"
              @change="crud.toQuery"
            >
              <el-option v-for="item in dict.info_verify_status" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
            <el-select
              v-model="query.isEnable"
              clearable
              size="small"
              placeholder="状态"
              class="filter-item"
              style="width: 80px"
              @change="crud.toQuery"
            >
              <el-option v-for="item in dict.data_status" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
            <rrOperation :crud="crud" />
          </div>
          <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right' ?-->
          <crudOperation :permission="permission" />

          <!--表格渲染-->
          <el-table
            ref="table"
            v-loading="crud.loading"
            :data="crud.data"
            size="small"
            style="width: 100%;"
            @selection-change="crud.selectionChangeHandler"
            @current-change="handleCurrentChange"
          >
            <el-table-column type="selection" width="50" />
            <el-table-column prop="name" label="标题" width="180" />
            <el-table-column prop="content" label="内容" />
            <el-table-column prop="publisher" label="发布者" width="100">
              <template slot-scope="scope">
                <div>{{ scope.row.publisher.username }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="发布时间" width="135">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="verifyStatus" label="审核状态" width="110">
              <template slot-scope="scope">
                <div v-if="scope.row.verifyStatus=='0'">待审核</div>
                <div v-if="scope.row.verifyStatus=='1'" style="color: limegreen">审核通过</div>
                <div v-if="scope.row.verifyStatus=='2'" style="color: #FF4949">审核未通过</div>
              </template>
            </el-table-column>
            <el-table-column prop="isEnable" label="状态" fixed="right" width="50">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.isEnable" active-color="#409EFF" inactive-color="#F56C6C" @change="changeEnabled(scope.row, scope.row.isEnable)" />
              </template>
            </el-table-column>
            <el-table-column
              v-permission="['admin','activities:edit','activities:del']"
              label="操作"
              width="120px"
              align="center"
              fixed="right"
            >
              <template slot-scope="scope">
                <udOperation :data="scope.row" :permission="permission" />
              </template>
            </el-table-column>
          </el-table>
          <!--分页组件-->
          <pagination />
        </el-card>
      </el-col>
    </el-row>
    <!-- 校庆活动详情 -->
    <el-row style="width: 100%;">
      <el-col :span="24">
        <el-card class="box-card">
          <div>
            <AppUserInfo ref="AppUserInfo" :permission="permission" />
          </div>
        </el-card>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import crudActivities from '@/api/anniversary/activities'
import CRUD, {
  presenter,
  header,
  form,
  crud
} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {
  mapGetters
} from 'vuex'
import AppUserInfo from '@/views/anniversary/activities/appUserInfo'

const defaultForm = {
  id: null,
  name: null,
  userId: null,
  content: null,
  startTime: null,
  endTime: null,
  appStartTime: null,
  appEndTime: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  sort: null,
  remark: null,
  isEnable: false,
  verifyStatus: '0'
}
export default {
  name: 'Activities',
  components: {
    AppUserInfo,
    pagination,
    crudOperation,
    rrOperation,
    udOperation
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['gender_type', 'data_status', 'political_climate', 'nation', 'department_name', 'in_gzmu', 'association_type',
    'info_verify_status'
  ],
  cruds() {
    return CRUD({
      title: '校庆活动',
      url: 'api/activities',
      sort: 'id,desc',
      crudMethod: { ...crudActivities
      }
    })
  },
  props: {
    // route object
    basePath: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      permission: {
        add: ['admin', 'activities:add'],
        edit: ['admin', 'activities:edit'],
        del: ['admin', 'activities:del'],
        verify: ['admin', 'activities:verify']
      },
      rules: {
        name: [{
          required: true,
          message: '标题不能为空',
          trigger: 'blur'
        }]
      },
      queryTypeOptions: [{
        key: 'name',
        display_name: '标题'
      },
      {
        key: 'content',
        display_name: '内容'
      }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      if (this.$refs.AppUserInfo) {
        this.$refs.AppUserInfo.query.activitiesId = ''
      }
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.id == null) {
        form.userId = this.user.id
        form.publishUserName = this.user.username
      } else {
        form.userId = `${form.publisher.id}`
        form.publishUserName = `${form.publisher.name}`
      }
      form.userType = `${form.userType}`
      form.verifyStatus = `${form.verifyStatus}`
      form.isEnable = `${form.isEnable}`
    },
    // resolvePath() {
    //   const routePath = 'http://localhost:8013/gzmu-anniversary/app_user_info'
    //   if (isExternal(routePath)) {
    //     return routePath
    //   }
    //   if (isExternal(this.basePath)) {
    //     return this.basePath
    //   }
    //   return path.resolve(this.basePath, routePath)
    // },
    // 选中校庆活动后，校庆活动详情数据与校庆活动预约人员基础信息详情
    handleCurrentChange(val) {
      // 设置基础信息
      if (val) {
        this.$refs.AppUserInfo.activitiesId = val.id
        this.$refs.AppUserInfo.query.activitiesId = val.id
        this.$refs.AppUserInfo.temp = val
        this.$refs.AppUserInfo.crud.toQuery()
      }
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.data_status[val] + '" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudActivities.edit(data).then(res => {
          this.crud.notify(this.dict.label.data_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.isEnable = !data.isEnable
        })
      }).catch(() => {
        data.isEnable = !data.isEnable
      })
    }
  }
}
</script>

<style scoped>

</style>
